package com.lanou.dllo.findfood.volley;

import android.widget.ImageView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.application.BaseApplication;

import java.util.Queue;

/**
 * Created by dllo on 15/10/22.
 */
public class VolleySington {
    private static VolleySington instance;//定义一个静态实例类对象
    private ImageLoader imageLoader;
    private RequestQueue queue;

    private  VolleySington(){
        queue = getQueue();//初始化方法
        imageLoader = new ImageLoader(queue,new DoubleCache());


    }
    public  static  VolleySington getInstance(){
        if (instance == null){
            synchronized (VolleySington.class){
                if (instance == null){
                    instance = new VolleySington();
                }
            }
        }
        return instance;
    }

 private    RequestQueue getQueue()
    {
        if (queue== null){
            queue = Volley.newRequestQueue(BaseApplication.getContext());
        }
        return  queue;
    }

    public  static  final String TAG = "VolleySington";
    public <T> void addRequest(Request<T> request){
        request.setTag(TAG);
        queue.add(request);
    }

    public <T> void addRequest(Request<T> request,Object tag){
        request.setTag(tag);
        queue.add(request);
    }

    public void addStringRequest(String url,Response.Listener listener,
                                 Response.ErrorListener error){
        StringRequest stringRequest = new StringRequest(url,listener,error);
        addRequest(stringRequest);

    }

    public void removeRequest(Object tag){
        queue.cancelAll(tag);
    }

    public ImageLoader getImageLoader() {
        return imageLoader;
    }



}




